﻿

using System;
using System.IO;
using gisgeb.comserver;
using NUnit.Framework;

namespace Tests
{
    [TestFixture]
    public class TheConfig
    {
        private static string file = "config.xml";
        
        [SetUp]
        public void SetUp()
        {
            if(File.Exists(file))
            {
                File.Delete(file);
            }
        }
        
        [Test]
        public void ShouldReturnsAStandardConfigIfNoConfigXmlExists()
        {
            Config cfg = Config.LoadFromFolder("");
        }
        
        [Test]
        public void ShouldThrowAConfigurationFileExceptionIfTheXmlIsInvalid()
        {
            File.WriteAllText("config.xml", "invalid xml...");
            Assert.Throws<ConfigurationFileException>(() => Config.LoadFromFolder(""));
        }
        
        [Test]
        public void WritingWorksAlsoButIsNotNeeded()
        {
            Config cfg = new Config();
            cfg.IgnoredPluginDllNames.Add("gisgeb.plugins.powershell.dll");
            cfg.SaveTo("c:\\temp\\test.config.xml");
        }

        [Test]
        public void ShouldReturnContainOneIgnoreIfTheXmlSpecifiesSo()
        {
            WriteConfigFromResource("Tests.TestData.WorkingConfig.xml");            
            Config cfg = Config.LoadFromFolder("");
            Assert.That(cfg.IgnoredPluginDllNames.Count, Is.EqualTo(1));
            Assert.That(cfg.IgnoredPluginDllNames[0], Is.EqualTo("gisgeb.plugins.powershell.dll"));
        }


        void WriteConfigFromResource(string resource)
        {
            using (TextReader r = new StreamReader(
                this.GetType().Assembly.GetManifestResourceStream(resource))) {
                {
                    File.WriteAllText(file, r.ReadToEnd());
                }
            }
        }
    }
}
